package com.zmn.plat.business.impl.servcategory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.servcategory.ShowCategoryAssociationBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.servcategory.ServCategorySimpleDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.servcategory.ShowCategoryAssociation;
import com.zmn.plat.model.entity.servcategory.ShowCategoryAssociationVO;
import com.zmn.plat.persistence.interfaces.servcategory.ShowCategoryAssociationDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linfeng
 * @version v1.0 @类说明：
 * @since 2019/09/22 15:51
 */
@Service
@Slf4j
public class ShowCategoryAssociationBServiceImpl implements ShowCategoryAssociationBService {

    @Resource
    ShowCategoryAssociationDAO showCategoryAssociationDAO;

    @Override
    public List<ShowCategoryAssociationVO> listByCategoryId(Integer servCategId, Integer categOneId, Integer categId) {
        if (NumberUtil.isNullOrZero(servCategId) && NumberUtil.isNullOrZero(categOneId) && NumberUtil.isNullOrZero(categId)) {
            log.error("params is all null or zero.");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        return showCategoryAssociationDAO.listByCategoryId(servCategId, categOneId, categId);
    }

    @Override
    public List<ShowCategoryAssociationVO> listByShowCategoryId(Integer showServCategId, Integer showCategOneId, Integer showCategId) {
        if (NumberUtil.isNullOrZero(showServCategId) && NumberUtil.isNullOrZero(showCategOneId) && NumberUtil.isNullOrZero(showCategId)) {
            log.error("params is all null or zero.");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        return showCategoryAssociationDAO.listByShowCategoryId(showServCategId, showCategOneId, showCategId);
    }

    @Override
    public List<ShowCategoryAssociationVO> listByShowCategoryIdsFroTariff(List<Integer> showCategIds) {
        if (CollectionUtils.isEmpty(showCategIds)) {
            return Collections.emptyList();
        }
        return showCategoryAssociationDAO.listByShowCategoryIdsFroTariff(showCategIds);
    }

    @Override
    public List<ServCategorySimpleDRO> listServCategByShowCategId(Integer showCategId) {
        if (NumberUtil.isNullOrZero(showCategId)) {
            log.error("showCategId is null or zero");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        List<ShowCategoryAssociationVO> voList = showCategoryAssociationDAO.listServCategByShowCategId(showCategId);
        BeanCopier copier = BeanCopier.create(ShowCategoryAssociationVO.class, ServCategorySimpleDRO.class, false);
        List<ServCategorySimpleDRO> dros = voList.stream().map(showCategoryAssociationVO -> {
            ServCategorySimpleDRO servCategorySimpleDRO = new ServCategorySimpleDRO();
            copier.copy(showCategoryAssociationVO, servCategorySimpleDRO, null);
            return servCategorySimpleDRO;
        }).collect(Collectors.toList());
        return dros;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchRelateBackCategory(String creater, JSONObject object) {
        String showServCategId = object.getString("showServCategId");
        String showCategOneId = object.getString("showCategOneId");
        String showCategId = object.getString("showCategId");
        JSONArray showCategoryAssociationList = object.getJSONArray("showCategoryAssociationList");
        List<ShowCategoryAssociation> list = new ArrayList<>();

        for (int i = 0; i < showCategoryAssociationList.size(); i++) {
            ShowCategoryAssociation showCategoryAssociation = new ShowCategoryAssociation();
            ShowCategoryAssociation result = JSONObject.parseObject(showCategoryAssociationList.getString(i), ShowCategoryAssociation.class);
            showCategoryAssociation.setShowGroupId(result.getShowGroupId());
            showCategoryAssociation.setGroupId(result.getGroupId());
            showCategoryAssociation.setServCategId(result.getServCategId());
            showCategoryAssociation.setShowServCategId(result.getShowServCategId());
            showCategoryAssociation.setCategOneId(result.getCategOneId());
            showCategoryAssociation.setShowCategOneId(result.getShowCategOneId());
            showCategoryAssociation.setCategId(result.getCategId());
            showCategoryAssociation.setShowCategId(result.getShowCategId());
            showCategoryAssociation.setCreater(creater);
            showCategoryAssociation.setCreateTime(DateUtil.getNow());
            list.add(showCategoryAssociation);
        }

        try {
            showCategoryAssociationDAO.deleteByShowServId(Integer.parseInt(showServCategId), Integer.parseInt(showCategOneId), Integer.parseInt(showCategId));
            showCategoryAssociationDAO.insertBatch(list);
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            log.error("关联已经存在，不能重复添加！");
            throw new PlatException(StatusConsts.STATUS_ERROR, "关联已经存在，不能重复添加！");
        } catch (Exception e) {
            log.error("deleteByShowServId error.", e);
            throw new PlatException(StatusConsts.STATUS_ERROR, e.getMessage());
        }
    }
}
